package com.backend.demo1.config;


import org.slf4j.event.Level;

public class ApiException extends RuntimeException {
    private final ApiResponse<?> apiResponse;
    private final Level logLevel;

    public ApiException(int code, String message) {
        this(code, message, determineLogLevelByCode(code));
    }

    // 允许自定义日志级别
    public ApiException(int code, String message, Level logLevel) {
        super(message);
        this.apiResponse = ApiResponse.error(code, message);
        this.logLevel = logLevel;
    }

    public ApiResponse<?> getApiResponse() {
        return apiResponse;
    }

    public Level getLogLevel() {
        return logLevel;
    }

    // 根据状态码返回对应的日志级别
    private static Level determineLogLevelByCode(int code) {
        if (code >= 500) {
            return Level.ERROR; // 服务器错误
        } else if (code >= 400) {
            return Level.WARN;  // 客户端错误
        } else {
            return Level.INFO;  // 其他情况（例如 2xx/3xx）
        }
    }
}